

package com.zy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.OSSObject;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

public class OssUtil {
    static String endPoint = "oss-cn-beijing.aliyuncs.com";
    static String accessKeyId = "LTAI5tQg8pm1cusa2dzPDB4h";
    static String accessKeySecret = "J9tT2YNq7CodRIHiIh51apxgzdYaZu";
    static String bucketName = "mall-233";

    public OssUtil() {
    }

    public static String upload(MultipartFile file) {
        String uploadUrl = null;

        try {
            OSSClient ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
            if (!ossClient.doesBucketExist(bucketName)) {
                ossClient.createBucket(bucketName);
                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }

            String filePath = (new SimpleDateFormat("yyyy/MM/dd")).format(new Date());
            String original = file.getOriginalFilename();
            String fileName = UUID.randomUUID().toString();
            String fileType = original.substring(original.lastIndexOf("."));
            String newName = fileName + fileType;
            String fileUrl = filePath + "/" + newName;
            ossClient.putObject(bucketName, fileUrl, file.getInputStream());
            ossClient.shutdown();
            uploadUrl = "http://" + bucketName + "." + endPoint + "/" + fileUrl;
        } catch (IOException var9) {
            var9.printStackTrace();
        }

        return uploadUrl;
    }

    public static void download(String filePath, HttpServletResponse response) {
        OSS ossClient = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        String fileName = filePath.substring(filePath.lastIndexOf(".com/") + 5);
        System.out.println(fileName);

        try {
            ossClient = new OSSClient(endPoint, accessKeyId, accessKeySecret);
            OSSObject ossObject = ossClient.getObject(bucketName, fileName);
            bufferedInputStream = new BufferedInputStream(ossObject.getObjectContent());
            bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
            byte[] bytes = new byte[2048];
            boolean var8 = true;

            int readNum;
            while((readNum = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, readNum);
            }

            bufferedOutputStream.flush();
        } catch (IOException var17) {
            var17.printStackTrace();
        } finally {
            try {
                ossClient.shutdown();
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }

                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException var16) {
                var16.printStackTrace();
            }

        }

    }
}
